package com.antler.smt.portal.service.impl;

import com.antler.smt.constant.LogisticsConstant;
import com.antler.smt.entity.DictionaryEntity;
import com.antler.smt.entity.OrderLogisticsEntity;
import com.antler.smt.enums.LogisticsEncodedEnum;
import com.antler.smt.portal.mapper.DictionaryInfoMapper;
import com.antler.smt.portal.mapper.OrderLogisticsInfoMapper;
import com.antler.smt.response.admin.GetOrderLogisticsResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.portal.OrderLogisticsService;
import com.antler.smt.utils.HttpUtil;
import com.antler.smt.utils.LogUtil;
import com.antler.smt.utils.LogisticsUtil;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * 快递物流实现接口
 *
 * @author zhangyi
 * @date 2018/4/12 10:25
 */
@Service
public class OrderLogisticsServiceImpl implements OrderLogisticsService {

    /**
     * 日志
     */
    private static Logger log = LogUtil.get();

    @Autowired
    private OrderLogisticsInfoMapper orderLogisticsInfoMapper;

    @Autowired
    private DictionaryInfoMapper dictionaryInfoMapper;

    @Override
    public Result<GetOrderLogisticsResp> getOrderLogisticsInfo(Long orderId) {
        Result result = null;

        //创建物流信息实体
        OrderLogisticsEntity orderLogisticsEntity = new OrderLogisticsEntity();

        //添加订单id
        orderLogisticsEntity.setOrderId(orderId);

        GetOrderLogisticsResp getOrderLogisticsResp = orderLogisticsInfoMapper.selectOrderLogisticsInfo(orderLogisticsEntity);

        result = new Result(getOrderLogisticsResp);

        return result;
    }

    /**
     * 查询实时的物流信息
     *
     * @param orderId
     * @return
     */
    @Override
    public Result actualTimelogisticsInfo(Long orderId) {
        Result result = null;

        //创建物流信息实体
        OrderLogisticsEntity orderLogisticsEntity = new OrderLogisticsEntity();

        //添加订单id
        orderLogisticsEntity.setOrderId(orderId);

        GetOrderLogisticsResp getOrderLogisticsResp = orderLogisticsInfoMapper.selectOrderLogisticsInfo(orderLogisticsEntity);

        //运单号
        String trackingNo = getOrderLogisticsResp.getTrackingNo();
        //物流公司
        String postCompanyName = getOrderLogisticsResp.getPostCompanyName();

        //快递字典
        String postCompnay = "post_compnay";

        DictionaryEntity dictionaryEntity = dictionaryInfoMapper.selectByKey(postCompnay);

        //获取json字符串
        String str = dictionaryEntity.getdValue();
        //获取快递编码
        String encoded = getEncoded(str, postCompanyName);

        String orderTraces = null;
        try {
            orderTraces = LogisticsUtil.getOrderTracesByJson(encoded, trackingNo);
            result = new Result(orderTraces);
        }catch (Exception e){
            log.error("快递查询失败！",e);
        }

        return result;
    }


    /**
     *  返回快递编码
     *
     * @param sourceStr
     * @return
     */
    public String getEncoded(String sourceStr,String companyName){
        String result = null;
        boolean falg = true;
        try {
            JSONArray jsonArray = new JSONArray(sourceStr);
            for(int i = 0; i < jsonArray.length() && falg; i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Integer id = (Integer)jsonObject.get("id");
                String company = (String) jsonObject.get("companyName");
                if(company.equals(companyName)){
                    for(LogisticsEncodedEnum logisticsEncodedEnum : LogisticsEncodedEnum.values()){
                        if(id.equals(logisticsEncodedEnum.key())){
                            result = logisticsEncodedEnum.desc();
                            falg = false;
                            break;
                        }
                    }
                }
            }
        }catch (JSONException e){
            e.printStackTrace();
        }
        return result;
    }

}
